package com.lianxi.Courses.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.DictConstant;
import com.lianxi.Courses.dao.GoldPointsMapper;
import com.lianxi.Courses.dao.UserMapper;
import com.lianxi.Courses.entity.*;
import com.lianxi.Courses.exception.PointErrorException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.GoldPointsService;
import com.lianxi.Courses.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 翔哥
 * 2025年10月13日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class GoldPointsServiceImpl implements GoldPointsService {

    @Autowired
    private GoldPointsMapper goldPointsMapper;

    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<GoldPoints> findPage(String id, String pageNo, String pageSize) {

        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.GOLDPPOINTS_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.GOLDPPOINTS_DEFAULT_PAGE_SIZE;
        }
        GoldPointsExample example = new GoldPointsExample();
        example.or()
                .andUserIdEqualTo(Integer.valueOf(id));


        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<GoldPoints> goldPoints = goldPointsMapper.selectByExample(example);

        PageInfo<GoldPoints> goldPointsPage = new PageInfo<>(goldPoints);
        return goldPointsPage;
    }

    @Override
    public User findGoldPoints(String id, HttpSession session) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        User user = userMapper.selectByPrimaryKey(Integer.valueOf(id));
        GoldPointsExample example = new GoldPointsExample();
        example.or()
                .andUserIdEqualTo(Integer.valueOf(id));
        List<GoldPoints> goldPoints = goldPointsMapper.selectByExample(example);
        for (GoldPoints goldPoints1:goldPoints){
            if (goldPoints1.getGoldCount()==0){
                user.setAllPoint(user.getAllPoint()+goldPoints1.getPointCount());
            }else if(goldPoints1.getPointCount()==0){
                user.setAllGold(user.getAllGold()+goldPoints1.getGoldCount());
            }
        }
        session.setAttribute("user",user);
        return user;
    }

    @Override
    public void pointBuyFind(String id, String point, HttpSession session) throws RequestParameterErrorException, PointErrorException {
        if (ParameterUtil.isNull(point) || ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        User user =findGoldPoints(id,session);
        if (user.getAllPoint()<Integer.valueOf(point)){
            throw new PointErrorException("积分不足,兑换失败");
        }
        if(Integer.valueOf(point)%10 != 0){
            throw new PointErrorException("请输入10的倍数的积分数");
        }

        //金币加
        GoldPoints goldPoints01 = new GoldPoints();
        goldPoints01.setUserId(user.getId());
        goldPoints01.setGoldCount((Integer.valueOf(point)/10));
        goldPoints01.setInfo(user.getNickname()+"用积分兑换的金币");
        goldPoints01.setCreateDate(new Date());

        //积分减
        GoldPoints goldPoints02 = new GoldPoints();
        goldPoints02.setUserId(user.getId());
        goldPoints02.setPointCount(-Integer.valueOf(point));
        goldPoints02.setInfo(user.getNickname()+"兑换金币消耗的积分");
        goldPoints02.setCreateDate(new Date());

        goldPointsMapper.insertSelective(goldPoints01);
        goldPointsMapper.insertSelective(goldPoints02);
        user =findGoldPoints(id, session);
        session.setAttribute("user" ,user);

    }
}
